/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.main.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.constant.OperationPlatform;
import com.tbit.common.entity.view.Result;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.OperaPswConstant;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.VO.RideOrderRefundReqVO;
import com.tbit.main.service.OperaPswService;
import com.tbit.main.service.RefundService;
import com.tbit.manager.api.RefundServiceApi;
import com.tbit.manager.api.dto.RefundReturnDTO;
import com.tbit.manager.api.dto.UserMoneyRefundDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 退款 Controller
 *
 * @author <a href="mailto:li.yao@tbit.com.cn">yao</a>
 * @since 2024/7/9 14:26
 */
@RestController
@RequestMapping("/refund")
public class RefundController {

    @Resource
    private RefundService refundService;
    @Autowired
    private RefundServiceApi refundServiceApi;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 骑行订单退款
     *
     * @param reqVO .
     */
    @PostMapping("/rideOrderRefund")
    @OperateLog(module = ModuleEnum.REFUND, methods = MethodEnum.RIDE_ORDER_REFUND_OPERATION)
    public JsonResult<String> rideOrderRefund(@Valid RideOrderRefundReqVO reqVO) {
        /**操作密码校验 兼容5.0.0之前版本 常量校验不通过时，再调接口校验*/
        if (!OperaPswConstant.DEPOSIT_OPERA_UPDATE_MONEY.equals(reqVO.getPsw())) {
            operaPswService.checkOperaPsw(LoginUserContextHolder.getLoginUser(), OperaPswEnum.OPERA_PSW_ORDER_CONFIG.getModule(),
                    reqVO.getPsw());
        }
        return JsonResult.succ(refundService.rideOrderRefund(reqVO));
    }

    /**
     * 用户余额退款
     *
     * @param userMoneyRefundDTO .
     */
    @GetMapping("/userMoneyRefund")
    public Result<RefundReturnDTO> userMoneyRefund(UserMoneyRefundDTO userMoneyRefundDTO){
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();
        /**操作密码校验 兼容5.0.0之前版本 常量校验不通过时，再调接口校验*/
        if (!OperaPswConstant.DEPOSIT_OPERA_UPDATE_MONEY.equals(userMoneyRefundDTO.getPsw())) {
            operaPswService.checkOperaPsw(loginUser, OperaPswEnum.OPERA_PSW_MONEY_CONFIG.getModule(), userMoneyRefundDTO.getPsw());
        }


        userMoneyRefundDTO.setLoginUserAccountUserId(loginUser.getAccountUserId());
        userMoneyRefundDTO.setLoginUserName(loginUser.getName());
        userMoneyRefundDTO.setLoginUserPhone(loginUser.getPhone());
        userMoneyRefundDTO.setLoginUserPlatform(OperationPlatform.MAINTAIN.code);
        Result<RefundReturnDTO> refundReturnDTOResult =
                refundServiceApi.userMoneyRefund(userMoneyRefundDTO);
        if (refundReturnDTOResult.unSuccess()){
            refundReturnDTOResult.setCode(0);
        }else {
            refundReturnDTOResult.setCode(1);
        }
        return refundReturnDTOResult;
    }

    /**
     * 退款结果查询
     *
     * @param refundBusinessNO 订单号
     */
    @GetMapping("/queryRefundResult")
    public Result<RefundReturnDTO> queryRefundResult(String refundBusinessNO){
        Result<RefundReturnDTO> refundReturnDTOResult =
                refundServiceApi.queryRefundResult(refundBusinessNO);
        if (refundReturnDTOResult.unSuccess()){
            refundReturnDTOResult.setCode(0);
        }else {
            refundReturnDTOResult.setCode(1);
        }
        return refundReturnDTOResult;
    }

}
